package com.zombie.logic.object;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.zombie.Constants;
import com.zombie.ResourceManager;
import com.zombie.effect.ImageEffect;
import com.zombie.logic.core.ZombieWorld;
import com.zombie.state.GameState;
import com.zombie.util.Utils;

public class BearZombie extends Zombie {

	int accelerationTime = 2000;
	long accelerationStarted = 0;
	boolean isAccelerated = false;

	
	public BearZombie(float x, float y, String image, int w, int h) {
		super(x, y, image, w, h);
	}

	public BearZombie(float x, float y, String image) {
		super(x, y, image,28,28);
	}
	
	@Override
	public void update(float delta) {
		super.update(delta);
		if (isDead() || wait)
			return;
		
		if (GameState.time-accelerationStarted > accelerationTime){
			isAccelerated = false;
		}
		
		if (isAccelerated){
			ImageEffect eff = new ImageEffect();
			eff.image = ResourceManager.getAnimation(image+"_move").getKeyFrame(GameState.time);
			eff.renderGroup = Constants.GROUP_PRE_EFFECT;
			eff.setBounds(position.x, position.y, size.x, size.y);
			eff.angle = angle;
			eff.opacity = 1f;
			eff.setFullLifeTime(500);
			ZombieWorld.addEffect(eff);
		}
	}
	
	public void draw(SpriteBatch batch, ShapeRenderer shapeBatch){
		if (isAccelerated){
			for(int i = 0; i < 2;i++){
				int x = -5+Utils.rnd.nextInt(10);
				int y = -5+Utils.rnd.nextInt(10);
				position.add(x, y);
				super.draw(batch, shapeBatch);
				position.add(-x, -y);
				Color old = batch.getColor();
				batch.setColor(Color.RED);
				super.draw(batch, shapeBatch);
				batch.setColor(old);
			}
		} else
			super.draw(batch, shapeBatch);
//		if (isAccelerated){
//			for(int i = 0; i < 2;i++){
//				int x = -3+Utils.rnd.nextInt(6);
//				int y = -3+Utils.rnd.nextInt(6);
//				g.rotate(pos.x+x, pos.y+y, (float) angle);
//				ResourceManager.getAnimation(image+"_move").getCurrentFrame().setAlpha(0.3f);
//				ResourceManager.getAnimation(image+"_move").getCurrentFrame().draw(pos.x+x-size.x/2, pos.y+y-size.y/2,size.x, size.y,Color.red);
//				ResourceManager.getAnimation(image+"_move").getCurrentFrame().setAlpha(1f);
//				g.rotate(pos.x+x, pos.y+y, (float) -angle);	
//			}
//		}
	}
	
	protected void onHit(GameObject damager){
		super.onHit(damager);
		if (accelerationStarted != 0 && GameState.time-accelerationStarted < accelerationTime)
			return;
		accelerate();
	}

	private void accelerate() {
		isAccelerated = true;
		accelerationStarted = GameState.time;
		playSound();
	}
	
	public float getVelocity(){
		if (isAccelerated)
			return super.getVelocity()*1.66f;
		return super.getVelocity();
	}
	
	public int getDamage() {
		if (isAccelerated)
			return (int) (super.getDamage()*1.66f);
		return super.getDamage();
	}
}
